package com.hs.srm.api.modules.requisition.controller;

import com.alibaba.fastjson.JSONObject;
import com.hs.srm.admin.modules.requisition.entity.ProPurchRequestHeaderEntity;
import com.hs.srm.admin.modules.requisition.entity.ProPurchRequestItemEntity;
import com.hs.srm.admin.modules.requisition.service.ProPurchRequestHeaderService;
import com.hs.srm.admin.modules.requisition.service.ProPurchRequestItemService;
import com.hs.srm.common.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

/**
 * @Description OEM申请单接口
 * @Author hp
 * @Date 2020-05-12
 */
@RestController
@RequestMapping("/api/OEMPurchRequest")
//@EnableAutoConfiguration
public class OEMPurchRequestApi {

    private Logger logger = LoggerFactory.getLogger(OEMPurchRequestApi.class);

    @Autowired
    private ProPurchRequestHeaderService proPurchRequestHeaderService;

    @Autowired
    private ProPurchRequestItemService ProPurchRequestItemService;

    /**
     * 该接口用于接收OEM申请单的数据
     *
     * @param data
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/dataFromJHT")
    public R OEMPurchRequest(@RequestParam("data") String data) throws UnsupportedEncodingException {
        //将获得的数据解码
        data = URLDecoder.decode(data, "UTF-8");
        logger.info("OEM申请单同步成功!" + data);
        List<ProPurchRequestItemEntity> itemList = JSONObject.parseArray(data, ProPurchRequestItemEntity.class);
        //将申请单行项目改成五位10->00010，物料编码截取000000000030120135->30120135
        itemList.forEach(item -> {
            item.setPurchRequestItemCode(String.format("%05d", Integer.valueOf(item.getPurchRequestItemCode())));
            item.setMaterialCode(item.getMaterialCode().replaceAll("^(0+)", ""));
            //srm收单日期
            item.setSrmReceiveDate(new Date());
            String[] str = item.getMaterialDesc().split("-");
            //拼接的数据不足，则不添加进去
            if (str.length < 4) {
                return;
            }
            //精度
            item.setJd(str[str.length - 1]);
            //阻值代码
            item.setZzdm(str[str.length - 2]);
            //功率
            item.setGl(str[str.length - 3]);
            //品名
            item.setPm(str[str.length - 4]);
        });
        ProPurchRequestHeaderEntity headerEntity = new ProPurchRequestHeaderEntity();
        //采购申请编码
        headerEntity.setPurchRequestCode(itemList.get(0).getPurchRequestCode());
        //采购申请类型,生产性为固定值，计划塔没有该字段，OEM申请暂时设定同生产性一致
        headerEntity.setPurchRequestType("ZBZ");
        //创建人
        headerEntity.setCreatedBy(itemList.get(0).getCreatedBy());
        //创建时间
        headerEntity.setCreationDate(itemList.get(0).getCreationDate());

        proPurchRequestHeaderService.insert(headerEntity);

        ProPurchRequestItemService.insertBatch(itemList);
        return R.ok();
    }
}
